!  BlackBerry 8300
!
!	Author:	Ross Barkman, hacking up by Tim Hatch and then Grant Goodale
!			[adapted/tested for the Blackberry 8300 by Monzilla Media]
!	History:
!		12 Sept 06	Initial release
!		11 June 07	Adapted for BB-8300
!
!  'mlts' resource info:
!    byte 1 == 01 -> modem HAS built-in error correction protocols
!    byte 2 == 01 -> modem HAS built-in data compression protocols
!    byte 3 == 200 -> max number of chars in varstr 7
!    byte 4 == 200 -> max number of chars in varstr 8
!    byte 5 == 200 -> max number of chars in varstr 9
!	
@ORIGINATE
@ANSWER
!
note "Connecting via BlackBerry 8300." 1
serreset 460800, 0, 8, 1
hsreset 0 0 0 0 0 0
settries 0
!
! Check the CrackBerry is responding
!
@LABEL 1
matchclr
matchstr 1 2 "OK\13\10"
write "ATZ\13"
matchread 30
inctries
iftries 3 101
jump 1
!
! Manufacturer check - not used
!
@LABEL 2
settries 0
!matchclr
!matchstr 1 3  "<manufacturer>"
!write "AT+CGMI\13"
!matchread 60
!jump 109
!
!
@LABEL 3
!
!
@LABEL 6
!
! Set additional options
!
matchclr
matchstr 1 10  "OK\13\10"
matchstr 2 108 "ERROR\13\10"
!write "AT&F&C1S0=0+IFC=3,1\13"
!matchread 30
!inctries
!iftries 3 101
!jump 6
!
! Check Access Point Name stored in dialstring (VarString 1)
! If APN is set to None, ignore it
!
ifstr 1 7 "None"
ifstr 1 7 "none"
ifstr 1 7 "NONE"
!
!	Set APN using +CGDCONT=1,"IP","<APN>"
!
write "AT+cgdcont=1,\34IP\34,\34^1\34\13"
jump 8
!
!
@LABEL 7
!
!	No APN
!
write "AT\13"
!
!
@LABEL 8
matchread 60
inctries
iftries 3 101
jump 6
!
!
@LABEL 9
!
! Subroutine: Attempt to reset the phone
!
!DTRClear
!pause 5
!DTRSet
!flush
!return
!
!
@LABEL 10
!
! Setup complete
!
ifANSWER 32
!
!
! ---- Place a call ----
!
@LABEL 20
!
!
note "Making EDGE connection." 3
write "ATDT*99#\13"
!
!
! ---- Connect response ----
!
! Parses successful connect responses in the following format:
!   CARRIER xxxxx
!	PROTOCOL: ppp
!	COMPRESSION: ccc
!   CONNECT zzzzz
!
@LABEL 32
matchclr
matchstr  1 81  "RING\13\10"
matchstr  2 102 "NO DIALTONE\13\10"
matchstr  3 103 "NO CARRIER"
matchstr  4 103 "ERROR\13\10"
matchstr  5 104 "BUSY\13\10"
matchstr  6 105 "NO ANSWER\13\10"
matchstr  7 57  "CARRIER"
matchstr  8 57  "CONNECT"
matchstr  9 61  "PROTOCOL"
matchstr 10 66  "COMPRESSION"
matchread 700
ifANSWER 32
jump 105
@LABEL 57
note "Communicating at 460800 bps." 2
CommunicatingAt 460800
jump 60
!
@LABEL 59
note "Communicating at an unknown rate." 2
!
@LABEL 60
jump 70
!
! -- PROTOCOL parsing --
!
! Userhook 2 informs ARA that a modem-to-modem error
! correcting protocol has been negotiated
!
@LABEL 61
matchclr
matchstr  1 63 "NONE"
matchstr  2 62 "LAPM"
matchstr  3 62 "ALT"
matchstr  4 63 "\13"
matchread 10
jump 63
!
@LABEL 62
note "Modem Reliable Link Established." 2
userhook 2
!
@LABEL 63
note "Modem Link Established." 2
jump 32
!
! -- COMPRESSION parsing --
!
! Userhook 3 informs ARA that a modem-to-modem compression
! protocol has been negotiated
!
@LABEL 66
matchclr
matchstr  1 68 "NONE"
matchstr  2 67 "V42B"
matchstr  3 67 "MNP5"
matchstr  4 68 "\13"
matchread 10
jump 68
!
@LABEL 67
note "Modem Compression Established." 2
userhook 3
!
@LABEL 68
note "No compression enabled." 2
jump 32
!
!
! -- Normal exit after "CONNECT" --
!
!
@LABEL 70
!
! Turn CTS on, for hardware flow control
!
HSReset 0 1 0 0 0 0
!
ifANSWER 71
pause 30
!
@LABEL 71
exit 0
!
!
! ---- Answer calls ----
!
!	A RING result from the modem and in ANSWERING mode
!	claims the serial port and answering the phone
!
@LABEL 81
ifORIGINATE 32
userhook 1
note "Answering phone..." 2
write "ATA\13"
jump 32
!
!
! ---- Hang up and reset modem ----
!
@HANGUP
@LABEL 90
settries 0
HSReset 0 0 0 0 0 0
!
@LABEL 92
!
!  Escape from data to command mode
!
matchclr
matchstr 1 94 "OK\13\10"
pause 11
write "+++"
matchread 20
!
@LABEL 94
!
! Send hangup command
!
pause 10
matchclr
matchstr 1 98 "NO CARRIER\13\10"
matchstr 2 98 "OK\13\10"
matchstr 3 98 "ERROR\13\10"
matchstr 4 98 "0\13\10"
write "ATH0\13"
matchread 30
! 
! If that fails, toggle DTR
!
DTRClear
pause 5
DTRSet
flush
!
! Try this three times only
!
inctries
iftries 3 101
jump 92
!
!
@LABEL 98
!
! Reset phone to default settings
!
pause 10
matchclr
matchstr 1 99 "OK\13\10"
write "AT&F\13"
matchread 30
jump 101
!
@LABEL 99
exit 0
!
! ---- Error messages -----
!
! Modem Not Responding
@LABEL 101
exit -6019
!
! No Dial Tone
@LABEL 102
exit -6020
!
! No Carrier or Error
@LABEL 103
exit -6021
!
! Busy
@LABEL 104
exit -6022
!
! No Answer
@LABEL 105
exit -6023
!
! User Cancellation
@LABEL 107
exit -6008
!
@LABEL 108
exit -6002 "The phone or network does not seem to support the GPRS options required for this script"
!
@LABEL 109
exit -6002 "This script is only suitable for <manufacturer> mobile phones"
!
@LABEL 110
exit -6002 "DO NOT put *99... in the telephone number - use the Access Point Name (APN) for your network instead"
